package patrol.mobile.com.mobilepatrol.activity.monitor;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.ImageAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.ImageBean;
import patrol.mobile.com.mobilepatrol.bean.ImageBean2;
import patrol.mobile.com.mobilepatrol.bean.ShebeiXunJianBean;
import patrol.mobile.com.mobilepatrol.bean.XjMingXiBean;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil02;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack2;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;

public class XjMingXiMuShowDetailActivity extends BaseActivity {
    @BindView(R.id.titleText)
    TextView titleText;
    @BindView(R.id.isOkText)
    RadioButton isOkText;
    //    @BindView(R.id.isOkImage)
//    ImageView isOkImage;
    @BindView(R.id.isBadText)
    RadioButton isBadText;
    //    @BindView(R.id.isBadImage)
//    ImageView isBadImage;
    @BindView(R.id.gzqkEdit)
    TextView gzqkEdit;
    @BindView(R.id.wxjlEdit)
    TextView wxjlEdit;
    @BindView(R.id.whjlEdit)
    TextView whjlEdit;
    @BindView(R.id.upPageText)
    TextView upPageText;
    @BindView(R.id.downPageText)
    TextView downPageText;
    @BindView(R.id.wxjlsjEdit)
    TextView wxjlsjEdit;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private XjMingXiBean xjMingXiBean;
    private ShebeiXunJianBean shebeiXunJianBean;
    private int position;
    private String id;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_deltail_xj_mingxi_show_layout;
    }

    @Override
    public void initData() {
        super.initData();
        position = getIntent().getIntExtra("position", 0);
        shebeiXunJianBean = (ShebeiXunJianBean) getIntent().getSerializableExtra("data");
        iv_back.setVisibility(View.VISIBLE);
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("巡检记录");
        wxjlsjEdit.setText(TimeUtils.timeString(System.currentTimeMillis()));
        wxjlsjEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerUtil02.getInstance(v.getContext()).setCallBack(new CallBack<DateBean>() {
                    @Override
                    public void onBack(DateBean dateBean) {
                        wxjlsjEdit.setText(dateBean.getmDate());
                    }
                });
            }
        });
        id = shebeiXunJianBean.getJiankkElefailinsprecDivList().get(position).getId();
        getHttpData();
    }


    @Override
    public void setListener() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        upPageText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == 0) {
                    ToastUtils.showToast("已经是第一页了");
                    return;
                }
                position--;
                id = shebeiXunJianBean.getJiankkElefailinsprecDivList().get(position).getId();
                getHttpData();
            }
        });
        downPageText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position >= shebeiXunJianBean.getJiankkElefailinsprecDivList().size()) {
                    ToastUtils.showToast("已经是最后一页了");
                    return;
                }
                position++;
                id = shebeiXunJianBean.getJiankkElefailinsprecDivList().get(position).getId();
                getHttpData();
            }
        });
    }

    private void getHttpData() {
        HashMap hashMap = new HashMap();
        hashMap.put("id", id);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getJiankkElefailinsprecDivEntity);
        sender.requestPost(hashMap, new HttpCallBack2(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response, String attachList) {
                xjMingXiBean = new Gson().fromJson(response, XjMingXiBean.class);
                titleText.setText(xjMingXiBean.getPosition() + "  " + xjMingXiBean.getEquipmentName());
                gzqkEdit.setText(xjMingXiBean.getFaultSituation());
                wxjlEdit.setText(xjMingXiBean.getMaintenanceLog());
                whjlEdit.setText(xjMingXiBean.getMaintenanceRecord());
                if (xjMingXiBean.getEquipmentStatus().equals("0")) {
//                    isOkImage.setImageResource(R.mipmap.danxuan_ok);
//                    isBadImage.setImageResource(R.mipmap.danxuan);
                    isOkText.setChecked(true);
                    isBadText.setChecked(false);
                } else {
                    isOkText.setChecked(false);
                    isBadText.setChecked(true);
                }

                if (attachList.length() > 10) {
                    recyclerView.setVisibility(View.VISIBLE);
                    List<ImageBean2> imageBeans = GsonUtil.jsonToList(attachList, ImageBean2.class);
                    Log.e("________", new Gson().toJson(imageBeans));
                    List<String> list = new ArrayList<>();
                    for (ImageBean2 imageBean : imageBeans) {
                        list.add((baseUrl.substring(0, baseUrl.length() - 1) + imageBean.getFilePath()).replace("\\", "/"));
                    }
                    recyclerView.setLayoutManager(new GridLayoutManager(XjMingXiMuShowDetailActivity.this, 4));
                    recyclerView.setAdapter(new ImageAdapter(list));
                } else {
                    recyclerView.setVisibility(View.GONE);
                }
            }

        });
    }
}
